package cn.ad.server.web.spring.service.actions.targeturl;

import cn.ad.server.web.spring.service.actions.command.TrackCommand;
import cn.ad.server.web.spring.service.actions.context.AdContext;
import cn.ad.server.web.spring.service.actions.context.BidContext;
import cn.ad.server.web.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 
 * 处理。
 * @author wangwei
 *
 */
@Component
public class ConvertTargetUrlHandler {
	
	private static Logger LOGGER = LoggerFactory.getLogger(ConvertTargetUrlHandler.class);
	
	@Resource(name="targetUrlConvert")
	public Map<String,IConvertTargetUrl> targetUrlConvert;
	
	
	/**
	 * 得到转换的targetUrl
	 * 
	 * @param bidContext
	 * @param entry
	 * @return
	 */
	public String getConvertTarget(BidContext bidContext, String eventValue) {
		String targetUrl ="";
		if (StringUtils.hasText(eventValue)) {
			//宏替换。
			TrackCommand command = new TrackCommand();
			populateCommand(bidContext, command);
			String targetUrl2 = eventValue;
			String targetUrlPrefix = HttpUtils.getUrlDomain(targetUrl2);
	
			//根据前缀得到合适的转换器进行转换。
			IConvertTargetUrl convert = (IConvertTargetUrl) targetUrlConvert.get(targetUrlPrefix);
			if(convert!=null){
				targetUrl2 = convert.convertTargetUrl(targetUrl2, command);
				targetUrl = targetUrl2;	
			}else{
				targetUrl = targetUrl2;
			}
		}
		return targetUrl;
	}
	
	/**
	 * 得到转换的targetUrl
	 * 
	 * @param bidContext
	 * @param entry
	 * @return
	 */
	public String getConvertTarget(AdContext bidContext, String eventValue) {
		String targetUrl ="";
		if (StringUtils.hasText(eventValue)) {
			//宏替换。
			TrackCommand command = new TrackCommand();
			populateCommand(bidContext, command);
			String targetUrl2 = eventValue;
			String targetUrlPrefix = HttpUtils.getUrlDomain(targetUrl2);
	
			//根据前缀得到合适的转换器进行转换。
			IConvertTargetUrl convert = (IConvertTargetUrl) targetUrlConvert.get(targetUrlPrefix);
			if(convert!=null){
				targetUrl2 = convert.convertTargetUrl(targetUrl2, command);
				targetUrl = targetUrl2;	
			}else{
				targetUrl = targetUrl2;
			}
		}
		return targetUrl;
	}
	
	/**
	 * 返回扩展url解析：
	 * "&mac={mac}&idfa={idfa}&imei={imei}
	 * @param bidContext
	 * @return
	 */
	public String convertExtendClickUrl(BidContext bidContext) {
//		StringBuilder builder = new StringBuilder();
//		builder.append("&mac=")
//		.append(null2Empty(bidContext.getMac()))
//		.append("&idfa=")
//		.append(null2Empty(bidContext.getIdfa()))
//		.append("&imei=")
//		.append(null2Empty(bidContext.getImei()));
//		return builder.toString();
		return "";
	}
	
	/**
	 * 返回扩展url解析
	 * "&mac={mac}&idfa={idfa}&imei={imei}
	 * @param bidContext
	 * @return
	 */
	public String convertExtendClickUrl(AdContext bidContext) {
//		StringBuilder builder = new StringBuilder();
//		builder.append("&mac=")
//		.append(null2Empty(bidContext.getMac()))
//		.append("&idfa=")
//		.append(null2Empty(bidContext.getIdfa()))
//		.append("&imei=")
//		.append(null2Empty(bidContext.getImei()));
//		return builder.toString();
		
		return "";
	}
	
	
	/**
	 * 填充TrackCommand
	 * 
	 * 注：STSAdRequestAction，STSAdProcessor，ImpressionHandler，ImpressionMatchService 中为同一份,有点不同。代码冗余。
	 * @param bidContext
	 * @param command
	 * @return
	 */
	protected TrackCommand populateCommand(AdContext bidContext,TrackCommand command){
		if(command==null){
			command = new TrackCommand();
		}
//		os={os}&mac={mac}&mac_md5={mac_md5}&idfa={idfa}
//		&idfa_md5={idfa_md5}&imei={imei}&imei_md5={imei_md5}&
//		androidid={androidid}&androidid_md5={androidid_md5}
		
//		&aid={aid}&sid={sid}&cid={cid}&mid={mid}&guid={guid}
		command.setOs(bidContext.getOs());
		command.setMac(bidContext.getMac1());
		command.setMac_md5(bidContext.getMac());
		command.setIdfa("");
		command.setIdfa_md5(bidContext.getIdfa());
		command.setImei("");
		command.setImei_md5(bidContext.getImei());
		command.setAndroidid("");
		command.setAndroidid_md5(bidContext.getAndroidId());
		command.setAid(bidContext.getAdSpaceId());
		command.setSid(bidContext.getStrategyId());
		command.setCid(bidContext.getCreativeId());
		command.setMid(bidContext.getMaterialId());
		command.setGuid(bidContext.getGuid());
		command.setAdvertising_id(bidContext.getAaid());
		
		return command;
	}
	
	
	/**
	 * 填充TrackCommand
	 * 
	 * 注：STSAdRequestAction，STSAdProcessor，ImpressionHandler，ImpressionMatchService 中为同一份,有点不同。代码冗余。
	 * @param bidContext
	 * @param command
	 * @return
	 */
	protected TrackCommand populateCommand(BidContext bidContext,TrackCommand command){
		if(command==null){
			command = new TrackCommand();
		}
//		os={os}&mac={mac}&mac_md5={mac_md5}&idfa={idfa}
//		&idfa_md5={idfa_md5}&imei={imei}&imei_md5={imei_md5}&
//		androidid={androidid}&androidid_md5={androidid_md5}
//		&aid={aid}&sid={sid}&cid={cid}&mid={mid}&guid={guid}
		command.setOs(bidContext.getOs());
		command.setMac(bidContext.getMac());
		command.setMac_md5(bidContext.getMacmd5());
		command.setIdfa(bidContext.getIdfa());
		command.setIdfa_md5(bidContext.getDpidmd5());
		command.setImei(bidContext.getImei());
		command.setImei_md5(bidContext.getImeimd5());
		command.setAndroidid(bidContext.getAnid());
		command.setAndroidid_md5(bidContext.getDpidmd5());
		command.setAid(0l);
		command.setSid(bidContext.getStrategyId());
		command.setCid(bidContext.getCreativeId());
		command.setMid(bidContext.getMaterialId());
		command.setGuid(bidContext.getGuid());
		command.setAdvertising_id(bidContext.getAaid());
		return command;
	}
	
	
	private String null2Empty(String targetUrl){
		if(targetUrl==null){
			targetUrl = "";
		}
		return targetUrl;
	}
	
	
}
